package com.alisls.demo.springcloud.service.user.web.menu;

import com.alisls.demo.springcloud.common.model.dto.DataResult;
import com.alisls.demo.springcloud.common.model.dto.Result;
import com.alisls.demo.springcloud.service.user.dto.resource.MenuDTO;
import com.alisls.demo.springcloud.service.user.service.resource.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 菜单管理
 *
 * @author Ke Wang
 * @date 2020/11/5
 */
@Api(tags = "菜单管理", description = "菜单管理API")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    /**
     * 保存菜单
     * @param menuDTO 菜单DTO
     * @return Result
     */
    @ApiOperation(value = "保存菜单", notes = "保存菜单信息")
    @PostMapping("/save")
    public Result save(@RequestBody @Validated MenuDTO menuDTO) {
        menuDTO = menuService.save(menuDTO);
        return DataResult.ofSuccess(menuDTO);
    }

    /**
     * 更新菜单
     * @param menuCode
     * @param menuDTO
     * @return
     */
    @ApiOperation(value = "更新菜单", notes = "根据菜单编码菜单信息")
    @PutMapping("/updateByMenuCode/{menuCode}")
    public Result updateByMenuCode(@PathVariable String menuCode,
                                     @RequestBody @Validated MenuDTO menuDTO) {
        menuDTO = menuService.update(menuDTO);
        return DataResult.ofSuccess(menuDTO);
    }

    /**
     * 根据菜单编码删除菜单
     * @param menuCode
     * @return
     */
    @DeleteMapping("/deleteByMenuCode/{menuCode}")
    public Result deleteByMenuCode(@PathVariable String menuCode) {
        Integer num = menuService.deleteByCode(menuCode);
        return DataResult.ofSuccess(num);
    }

}
